#!/bin/bash


# 从新启动
# sh ./sh/restart_redis_group_consumer_server.sh  wx792152cf7d4b3d45  visit_log_ceshi

# 当前脚本所在的目录
# script_dir=$(dirname $0)
# script_dir=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
script_dir=$(cd $(dirname "$0") && pwd)
# 当前脚本名称 https://www.v2ex.com/t/302728
script_name=$(basename $0)
echo "script_dir  ${script_dir}  $0 $(basename $0)"

SHELL_FOLDER=$(dirname $(readlink -f "$0"))
echo "script_dir  ${SHELL_FOLDER} $(readlink -f "$0") $BASH_ARGV "



if [ -n "$BASH_SOURCE" ]; then
    current_script_dir=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
else
    current_script_dir=$(cd $(dirname "$0") && pwd)
fi

echo "current_script_dir  ${current_script_dir}  "


# ps -aux
# USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
# root      5328  0.0  0.0 113284  1208 pts/4    S    10:22   0:00 sh ./sh/queue_redis_group_consumer_msg_server.sh wx00f1e81346104aaa visit_log
# root      5329  3.0  0.6 292240 27024 pts/4    S    10:22   0:00 php artisan command:queue_redis_group_consumer_msg_server --env=wx00f1e81346104aaa --group_name=visit_log
# root     22256  0.0  0.0 113284  1196 ?        S    1月04   0:00 sh ./sh/queue_redis_group_consumer_msg_server.sh wx00f1e81346104aaa share_log
# root     22257  0.0  0.7 294268 28028 ?        S    1月04   1:11 php artisan command:queue_redis_group_consumer_msg_server --env=wx00f1e81346104aaa --group_name=share_log
#
#
#

appid_env=$1
group_name=$2


# ps -aux | grep "${appid_env}" | grep "${group_name}"
# ps -aux | grep "wx00f1e81346104aaa" | grep "visit_log"
# root      5328  0.0  0.0 113284  1208 ?        S    10:22   0:00 sh ./sh/queue_redis_group_consumer_msg_server.sh wx00f1e81346104aaa visit_log
# root      5329  0.1  0.7 294288 29112 ?        S    10:22   0:00 php artisan command:queue_redis_group_consumer_msg_server --env=wx00f1e81346104aaa --group_name=visit_log


# ps -aux | grep "${appid_env}" | grep "${group_name}" | awk '{print $2}'
# ps -aux | grep 'wx00f1e81346104aaa' | grep 'visit_log' | awk '{print $2}'
# 5328
# 5329

echo "-----------"


# 这么写总是会多出 pid的进程 因为它把自己也算进去了 刚好符合下面的过滤指令
# root      1019  0.0  0.0   2392   732 pts/1    S+   16:09   0:00 sh ./sh/restart_redis_group_consumer_server.sh wx792152cf7d4b3d45 visit_log_ceshi
#
# ps -aux | grep "${appid_env}" | grep "${group_name}" | awk '{print $2}' | xargs kill -9
# pdis=`ps -aux | grep "${appid_env}" | grep "${group_name}" | awk '{print $2}' `
# pdis=`ps -aux | grep "${appid_env}" | grep "${group_name}" | cut -c 9-15 `
# pdis=` ps -aux | grep "${appid_env}" | grep "${group_name}" |  cut -c 9-15 `
# pdis=` ps -aux | grep "${appid_env}" | grep "${group_name}" `

# 优化改写过滤指令 一 再加一层过滤
# pdis=` ps -aux | grep "${appid_env}" | grep "${group_name}" |  grep 'queue_redis_group_consumer_msg_server' | cut -c 9-15 `
# 优化改写过滤指令 二 排除当前脚本文件的所在的命令 [强烈推荐]
# pdis=` ps -aux |  grep "${appid_env}" | grep "${group_name}" |  grep -v 'restart_redis_group_consumer_server.sh' | awk '{print $2}' `
# pdis=` ps -aux |  grep "${appid_env}" | grep "${group_name}" |  grep -v "${script_name}" | cut -c 9-15 `
# pdis=` ps -aux |  grep "${appid_env}" | grep "${group_name}" |  grep -v "${script_name}" | awk '{print $2}' `
pdis=` ps -ef |  grep "${appid_env}" | grep "${group_name}" |  grep -v "${script_name}" | awk '{print $2}' `
echo 'found ID list:' ${pdis}
echo " "






# -n 判断一个变量有值 为真
if [ -n "$pdis" ]; then
    for mypid in $pdis
    do
        # 杀掉进程
        kill -9 $mypid
        echo "killed $mypid"
    done
fi

# echo "------====-----"

# 有个问题 执行完上面的 kill 指令后，没有继续往下运行代码
# 因为代码写错了 导致的

if [ $? -ne 0 ]; then
    echo "killed failed"
else
    echo "killed succeed"
fi

echo "------====-----"


# https://blog.csdn.net/qq_31382921/article/details/79817072
# https://www.cnblogs.com/f-ck-need-u/p/7427262.html
# sleep 1      睡眠1秒
# sleep 1s     睡眠1秒
# sleep 1m     睡眠1分
# sleep 1h     睡眠1小时
# sleep默认的休眠单位为秒，因此上面表示休眠1秒钟。如果要休眠毫秒级、微秒级，则可以使用小数。例如：
# sleep 0.5    表示休眠半秒钟。
#
# sleep 0.01   睡眠 10ms
# sleep 0.1   睡眠 100ms
# 此外，还有专门的微秒级的休眠命令usleep。例如：
# usleep 1000  表示休眠1000微秒，即1毫秒
# usleep 10000 睡眠 10ms usleep为十的负六次方秒，详细见man usleep






sleep 0.01s
sh ./sh/queue_redis_group_consumer_msg_server.sh ${appid_env} ${group_name}  >> "${appid_env}_${group_name}.txt" &

if [ $? -ne 0 ]; then
    echo "start failed"
else
    echo "start succeed"
fi


sleep 0.05s
# ps -aux |  grep "${appid_env}" | grep "${group_name}" | grep -v "${script_name}"
ps -ef |  grep "${appid_env}" | grep "${group_name}" | grep -v "${script_name}"





